/**
 * role edit
 */
layui.use(['form', 'tableSelect'], function () {
    let form = layui.form,
        layer = layui.layer,
        tableSelect = layui.tableSelect,
        $ = layui.$;

    let u = localStorage.getItem("ADMIN_LOGIN");
    let loginU = {};
    if (u == undefined) {
        layer.msg('请登录', {icon: 1, time: 500}, function () {
            window.location = 'page/login.html';
        });
    } else {
        loginU = decryptLocalStorage(u);
    }
    let _data = localStorage.getItem("roleData");
    let viewData = undefined;
    if (_data != undefined) {
        viewData = decryptLocalStorage(_data, loginU.token);
        $("#shipId").val(viewData.shipId);
        $("#shipName").val(viewData.shipName);
        $("#shipIds").val(viewData.shipIds);
        $("#shipNames").val(viewData.shipNames);
        $("#roleName").val(viewData.roleName);
        $("#roleName").attr("disabled", "disabled");
        $("#shipName").attr("disabled", "disabled");
    }
    //监听提交
    form.on('submit(saveBtn)', function (data) {
        var params = data.field;
        params.token = loginU.token;
        let shipIdArray = params.shipIds.split(",");
        let shipIdFlag = true;
        shipIdArray.forEach(function (idx, id) {
            if (params.shipId == id) {
                shipIdFlag = false;
            }
        });
        if (!shipIdFlag) {
            shipIdArray.push(params.shipId);
        }
        params.shipIds = shipIdArray.join(",")
        let shipNameArray = params.shipNames.split(",");
        let shipNameFlag = true;
        shipNameArray.forEach(function (idx, name) {
            if (params.shipName == name) {
                shipNameFlag = false;
            }
        });
        if (!shipNameFlag) {
            shipNameArray.push(params.shipName);
        }
        params.shipNames = shipNameArray.join(",");
        console.log(JSON.stringify(params));
        //添加
        if (viewData == undefined) {
            request.add('role', JSON.stringify(params), function (result) {
                if (result.code == 0) {
                    layer.msg('保存成功', {
                        icon: 1,
                        time: 500
                    }, function () {
                        // 关闭弹出层
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    });

                } else {
                    layer.msg('保存失败', {
                        icon: 2,
                        time: 2000
                    });
                }
            });
        }
        //修改
        else {
            params.roleId = viewData.roleId;
            request.update('role', JSON.stringify(params), function (result) {
                if (result.code == 0) {
                    layer.msg('保存成功', {
                        icon: 1,
                        time: 500
                    }, function () {
                        // 关闭弹出层
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    });

                } else {
                    layer.msg('保存失败', {
                        icon: 2,
                        time: 2000
                    });
                }
            });
        }

        return false;
    });

    let shipId = "";
    let shipName = "";
    if (loginU.shipId != 0) {
        shipId = loginU.shipId
        shipName = loginU.shipName;
    }
    if (loginU.userType != '1') {
        $("#shipId").val(loginU.shipId);
        $("#shipName").val(loginU.shipName);
        $("#shipName").attr("disabled", "disabled");
    }

    let shipTableUrl = "/admin/v1/ship/page?shipStatus=营运&token=" + loginU.token + "&shipId=" + shipId;
    tableSelect.render({
        elem: '#shipName',
        checkedKey: 'id',
        searchFlag: true,
        searchKey: 'shipName',
        table: {
            url: shipTableUrl,
            cols: [[
                {type: 'radio'},
                {field: 'shipId', title: 'ID'},
                {field: 'shipCompanyName', title: '船舶公司'},
                {field: 'shipName', title: '船舶'}
            ]]
        },
        done: function (elem, data) {
            if (data.data.length > 0) {
                $("#shipId").val(data.data[0].shipId);
                $("#shipName").val(data.data[0].shipName);
                $("#shipIds").val(data.data[0].shipId);
                $("#shipNames").val(data.data[0].shipName);
            }
        },
        clear: function () {
            $("#shipId").val("");
            $("#shipName").val("");
            $("#shipIds").val("");
            $("#shipNames").val("");
        }
    })


    let tableUrl = "/admin/v1/ship/page?token=" + loginU.token + "&shipId=" + shipId;
    tableSelect.render({
        elem: '#shipNames',
        checkedKey: 'id',
        searchFlag: true,
        searchKey: 'shipName',
        table: {
            url: tableUrl,loginUserType:loginU.userType,
            cols: [[
                {type: 'checkbox'},
                {field: 'shipId', title: 'ID'},
                {field: 'shipCompanyName', title: '船舶公司'},
                {field: 'shipName', title: '船舶'}
            ]]
        },
        done: function (elem, data) {
            if (data.data.length > 0) {
                let shipIds = $("#shipIds").val();
                let shipNames = $("#shipNames").val();
                let shipIdArray = [];
                let shipNameArray = [];
                if (shipIds != '' && shipNames != '') {
                    shipIdArray = shipIds.split(",");
                    shipNameArray = shipNames.split(",");
                }
                layui.each(data.data, function (index, ship) {
                    let shipId = ship.shipId;
                    let shipName = ship.shipName;
                    shipIdArray.push(shipId);
                    shipNameArray.push(shipName);
                });
                $("#shipIds").val(shipIdArray.join(","));
                $("#shipNames").val(shipNameArray.join(","));
            }
        },
        clear: function () {
            $("#shipIds").val("");
            $("#shipNames").val("");
        }
    })


});
